package mahjong.auto.com.automahjong;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限处理类
 * Created by heqian on 17-4-27.
 */

public class Permission {
    private int requestCode;
    private Activity activity;
    private List<String> permissionList = new ArrayList<>();
    private List<String> permission = new ArrayList<>();

    public Permission(Activity activity, int requestCode) {
        this.activity = activity;
        this.requestCode = requestCode;
    }

    public void addPermission(String... permission) {
        for (String item : permission) {
            this.permission.add(item);
        }
    }

    public boolean isCheck() {
        for (String item : permission) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(item);
            }
        }

        return 0 == permissionList.size();
    }

    public void request() {
        if (0 < permissionList.size()) {
            String[] list = new String[permissionList.size()];
            permissionList.toArray(list);
            ActivityCompat.requestPermissions(activity, list, requestCode);
        }
    }

    public boolean isCheck(String[] permiss, int[] grantResults) {
        for (String item : permission) {
            for (int i = 0; i < permiss.length; i++) {
                if (permiss[i].equals(item) && grantResults[i] != PackageManager.PERMISSION_GRANTED)
                    return false;
            }
        }
        return true;
    }
}